#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QTextStream>
#include <QFileDialog>
#include <QTextEdit>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QLabel>
#include <QVector>
#include <QStringList>
#include <QVBoxLayout>
#include <QHBoxLayout>
//#include <QLayout>
#include "qcustomplot.h"
#include <QtNetwork/QTcpSocket>

#define FLAG_TCP_STATUS_MASK 0x03
#define FLAG_TCP_STATUS_CONNECTED 0x01
#define FLAG_TCP_STATUS_DISCONNECTED 0x00

#define FLAG_CNC_BUSY_MASK 0x04
#define FLAG_CNC_BUSY_TRUE 0x04
#define FLAG_CNC_BUSY_FALSE 0x00

#define FLAG_FILE_OPENED_MASK 0x08
#define FLAG_FILE_OPENED_TRUE 0x08
#define FLAG_FILE_OPENED_FALSE 0x00

#define FLAG_INTERPOLATOR_WORK_MASK 0x10
#define FLAG_INTERPOLATOR_WORK_TRUE 0x10
#define FLAG_INTERPOLATOR_WORK_FALSE 0x00

#define FLAG_START_MASK 0x20
#define FLAG_START_TRUE 0x20
#define FLAG_START_FALSE 0x00

#define FLAG_PAUSE_MASK 0x40
#define FLAG_PAUSE_TRUE 0x40
#define FLAG_PAUSE_FALSE 0x00

//float x_unit = 0.02;
//float y_unit = 0.02;
//float z_unit = 0.02;
#define X_UNIT 0.02f
#define Y_UNIT 0.02f
#define Z_UNIT 0.02f

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QLineEdit * FeedSpeedValue;
    QLineEdit * RotationValue;
    QLabel    * FeedSpeedLabel;
    QLabel    * RotationLabel;
    QLabel    * MainsharftStatusLabel;
    QLabel    * CoolingStatusLabel;
    QLabel    * MainsharftStatusImage;
    QLabel    * CoolingStatusImage;
    QPixmap pixMapGreen;
    QPixmap pixMapRed;
    float feedspeed;
    float mainshaftSpeed;
    int coolingStatus=0;
    int mainshaftStatus=0;
    void DrawPreView_AddPoint(float x,float y,float z);
    void DrawWork_AddPoint(float x,float y,float z);
    void SetStatusSignal(int status);
private slots:
    void openFile(void);
    void MDI_Enter_Slot(void);
    void PlotUpdate_Slot(void);
    void Interpolate_Slot(void);
    void Start_Slot(void);
    void Pause_Slot(void);
    void Stop_Slot(void);
    void Close_Slot(void);
private:
    QAction *act_open;
    QAction *act_close;
    QAction *act_exit;
    QAction *act_start;
    QAction *act_pause;
    QAction *act_stop;
    QMenu * menu_file;
    QMenu * menu_edit;
    QMenuBar * menuBar;
    QToolBar * toolBar;
    QTextStream G_Code_Stream;
    QTextEdit *textEdit;
    QGridLayout * MainLayout;
    QGridLayout * StatusLayout;
    QHBoxLayout * SpindleSpeedLayout;
    QHBoxLayout * SpindleStatusLayout;
    QHBoxLayout * FeedSpeedLayout;
    QHBoxLayout * CoolingStatusLayout;

    QLabel      * MDI_Label;
    QVBoxLayout * MDI_Layout;


    QCustomPlot * widgetDrawArea;
    QLineEdit * lineEdit_MDI;
//    QLineEdit * lineEdit_Test;


    QCPGraph *PreviewCurve;
    QCPGraph *WorkCurve;
    QString G_Code_Line;
    QTcpSocket *client;
    QVector<double> preview_x_data;
    QVector<double> preview_y_data;
    QVector<double> work_x_data;
    QVector<double> work_y_data;
    QTimer *FlushTimer;
    QTimer *InterpolateTimer;
    QStringList G_Code_list;
    unsigned int Flags;
    int total_line;
    int current_line;
//    char sendbuf[100];
    void UI_Init(void);
    void SignalConfig(void);
    void PlotInit(QCustomPlot *ploter);
    void WorkGraphActive(void);
    void PreviewGraphActive(void);
    void TimerInit(void);
    void SelectNextLine(void);
    void C_CallbackConfig(void);
    void ProcessGCodeFormQString(QString *qstring);
//    void Send_G_Code_Line(QString *G_Code);
//    void Recieve_Handler(QString *recv);
};

#endif // MAINWINDOW_H
